package org.example.xtcs.anpes1;

import com.google.auto.service.AutoService;
/*import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Names;*/
import org.example.datasoure.dataSource.DbUtil;
import org.example.xtcs.annotate.Xtcs;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

/**
 * @ClassName: CustomAnnotationProcessor
 * @Description:
 * @Author: YourName
 * @Date: 2024/12/18
 */

//@SupportedAnnotationTypes("org.example.xtcs.annotate.Xtcs")
//@SupportedSourceVersion(SourceVersion.RELEASE_8)
//@AutoService(Processor.class)
public class XtcsAnnotationProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        return false;
    }/*
    java.util.List<String> fieldNames = new ArrayList<>(Arrays.asList("XTCS1", "XTCS2", "XTCS3"));


    private JavacTrees javacTrees; // 获取 JcTree
    private TreeMaker treeMaker; // 构建生成 jcTree
    private Names names;


    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        System.out.println("【Xtcs】编译注释处理器");
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "这是我的处理器");
        javacTrees = JavacTrees.instance(processingEnv);// 语法树
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
        this.treeMaker = TreeMaker.instance(context);
        super.init(processingEnv);
        this.names = Names.instance(context);

    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

        for (Element element : roundEnv.getElementsAnnotatedWith(Xtcs.class)) {
            // 获取字段信息
            String className = element.getSimpleName().toString();
            TypeMirror fieldType = element.asType();
            System.out.println("类路径-------------" + fieldType.toString());
            JCTree tree = javacTrees.getTree(element);
            JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl) tree;

            try {
                DbUtil.connect02();
            } catch (ClassNotFoundException | SQLException | IllegalAccessException | InstantiationException e) {
                throw new RuntimeException(e);
            }

            //生成内部类
            fieldNames.stream().forEach(fieldName -> {
                createClass(classDecl, fieldName);
            });
        }
        return true;
    }

    private JCTree.JCVariableDecl createField(String fieldName, String value) {
        return treeMaker.VarDef(
                treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL),
                names.fromString(fieldName),
                treeMaker.Ident(names.fromString("String")),
                treeMaker.Literal(value)
        );
    }


    private void createClass(JCTree.JCClassDecl classDecl, String className) {
        JCTree.JCClassDecl jcClassDecl = treeMaker.ClassDef(
                treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL),
                names.fromString(className),
                List.<JCTree.JCTypeParameter>nil(),
                null,
                List.<JCTree.JCExpression>nil(),
                List.<JCTree>nil()
        );
        //java.lang.AssertionError: Value of x -1
        treeMaker.pos = classDecl.pos;
        //内部类生成字段
        JCTree.JCVariableDecl field = createField("csdm", className+"_xtcs");
        jcClassDecl.defs = jcClassDecl.defs.append(field);
        //添加方法
        JCTree.JCMethodDecl get = createMethodNoP(className,"get1");
        System.out.println("添加get方法============" + get.toString());

        jcClassDecl.defs = jcClassDecl.defs.append(get);

        System.out.println("添加get方法后===========" + jcClassDecl.toString());

        *//*JCTree.JCMethodDecl jcMethodDecl = createMethodPR("get" + JavaTreeUtil.capitalize(className));
        System.out.println("添加有参数有返回值方法" + jcMethodDecl.toString());
        jcClassDecl.defs = jcClassDecl.defs.append(jcMethodDecl);

        JCTree.JCMethodDecl methodNoPNoR = createMethodNoPNoR("get" + JavaTreeUtil.capitalize(className));
        System.out.println("添加无参数无返回值方法" + methodNoPNoR.toString());
        jcClassDecl.defs = jcClassDecl.defs.append(methodNoPNoR);*//*
        //添加静态内部类
        classDecl.defs = classDecl.defs.append(jcClassDecl);
    }


    private JCTree.JCMethodDecl createMethodPR(String methodName) {
        //有参有返回值
        ListBuffer<JCTree.JCStatement> testStatement3 = new ListBuffer<>();
        testStatement3.append(treeMaker.Return(treeMaker.Ident(names.fromString("name"))));
        //生成方法体
        JCTree.JCBlock testBody3 = treeMaker.Block(0, testStatement3.toList());
        //参数列表
        JCTree.JCVariableDecl param3 = treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER),
                names.fromString("name"), treeMaker.Ident(names.fromString("String")),
                null);
        com.sun.tools.javac.util.List<JCTree.JCVariableDecl> parameters3 = com.sun.tools.javac.util.List.of(param3);
        return treeMaker.MethodDef(
                // 方法限定值
                treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC),
                // 方法名
                names.fromString(methodName),
                // 返回类型
                treeMaker.Ident(names.fromString("String")),
                List.<JCTree.JCTypeParameter>nil(),
                // 入参
                parameters3,
                List.<JCTree.JCExpression>nil(),
                // 方法体
                testBody3,
                null
        );
    }


    private JCTree.JCMethodDecl createMethodNoPNoR(String methodName) {
        ListBuffer<JCTree.JCStatement> testStatement = new ListBuffer<>();
        JCTree.JCBlock testBody = treeMaker.Block(0, testStatement.toList());
        return treeMaker.MethodDef(
                treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC),
                // 方法限定值
                names.fromString(methodName),
                // 方法名
                treeMaker.TypeIdent(TypeTag.VOID),
                // 返回类型
                List.<JCTree.JCTypeParameter>nil(),
                List.<JCTree.JCVariableDecl>nil(),
                List.<JCTree.JCExpression>nil(),
                testBody,
                // 方法体
                null);
    }


    private JCTree.JCMethodDecl createMethodNoP(String ClassName,String methodName) {
        ListBuffer<JCTree.JCStatement> testStatement3 = new ListBuffer<>();
        testStatement3.append(treeMaker.Return(
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        select("Student.get"),
                        List.<JCTree.JCExpression>of(select(ClassName+".csdm"))
                )
        ));
        JCTree.JCBlock testBody = treeMaker.Block(0, testStatement3.toList());
        return treeMaker.MethodDef(
                treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC),
                // 方法限定值
                names.fromString(methodName),
                // 方法名
                treeMaker.Ident(names.fromString("XtcsVo")),
                // 返回类型
                List.<JCTree.JCTypeParameter>nil(),
                List.<JCTree.JCVariableDecl>nil(),
                List.<JCTree.JCExpression>nil(),
                testBody,
                // 方法体
                null);
    }


    private void createMethod(String methodName, String expressive) {
        JCTree.JCExpressionStatement exec2 = treeMaker.Exec(
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        null,
                        List.<JCTree.JCExpression>nil()
                )
        );
    }


    private JCTree.JCFieldAccess select(String expressive) {
        String[] exps = expressive.split("\\.");
        JCTree.JCFieldAccess access = treeMaker.Select(ident(exps[0]), names.fromString(exps[1]));
        int index = 2;
        while (index < exps.length) {
            access = treeMaker.Select(access, names.fromString(exps[index++]));
        }
        return access;
    }

    private JCTree.JCIdent ident(String name) {
        return treeMaker.Ident(names.fromString(name));
    }*/
}
